package project.bluetooth.com.loadsirproject.network.model.impl;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;
import project.bluetooth.com.loadsirproject.network.bean.BResponse;
import project.bluetooth.com.loadsirproject.network.callback.IResultCallback;
import project.bluetooth.com.loadsirproject.network.model.IHttpModel;

/**
 * Created by lgs on 2018/3/23.
 * Post请求M层的实现类
 */

public class HttpPostModelImpl implements IHttpModel {

    @Override
    public void requestData(final Context context, String urlPath, Map<String, String> map, final IResultCallback resultCallback) {

        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("X-Requested-With", "XMLHttpRequest");
//        headerMap.put("Authorization", PreferenceCache.getToken());

        OkHttpUtils.post()
                .url(urlPath)
                .headers(headerMap)
                .params(map)
                .build()
                .execute(new StringCallback() {

                    @Override
                    public void onBefore(Request request, int id) {

//                        //判断网络是否可用
//                        if (!NetworkUtils.isNetworkConnected(context)) {
//                            Toast.makeText(context, "请检查网络连接状态", Toast.LENGTH_SHORT).show();
//                        }
                    }

                    //解析数据就返回true
                    @Override
                    public boolean validateReponse(Response response, int id) {

                        return true;
                    }

                    @Override
                    public void onResponse(String json, int id) {
                        resultCallback.onResponse(json);
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                        resultCallback.onError(e.getMessage());
                        e.printStackTrace();
                        Log.e("tagLog", e.getMessage());
                        //隐藏加载框
                    }

                    @Override
                    public String parseNetworkResponse(Response response, int id) throws IOException {

                        if (response.code() >= 200 && response.code() < 300) {
                            return response.body().string();
                        } else {
                            String string = response.body().string();
                            Gson gson = new Gson();
                            BResponse errorResponse = gson.fromJson(string, BResponse.class);
                            String msg = errorResponse.getMsg();

                            throw new IOException("code is:" + response.code() + "\n" + msg);
                        }
                    }

                    @Override
                    public void onAfter(int id) {
                        super.onAfter(id);
                        //隐藏加载框
                    }
                });


    }
}
